package com.example.demo.lc;

import com.google.common.collect.Lists;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Description TODO
 * @Author wzh
 * @Date 2021/9/7 12:48 下午
 * @Version 1.0
 */
public class leetCode139 {
    public static void main(String[] args) {
        //egg1
        /*String s = "leetcode";
        List<String> wordDict = Lists.newArrayList("leet","code");*/
        //egg2
        /*String s = "applepenapple";
        List<String> wordDict = Lists.newArrayList("apple","pen");*/
        //egg3
        String s = "bb";
        List<String> wordDict = Lists.newArrayList("a", "b", "bbb", "bbbb");
        System.out.println(wordBreak(s, wordDict));


    }

    /**
     *
     * @param s
     * @param wordDict
     * @return
     */
    public static boolean wordBreak(String s, List<String> wordDict) {
        Set<String> set = new HashSet<>(wordDict);
        boolean[] dp = new boolean[s.length() + 1];
        dp[0] = true;
        for (int i = 0; i < s.length(); i++) {
            for (int j = 0; j < i; j++) {
                if (dp[j] && set.contains(s.substring(j, i))) {
                    dp[i] = true;
                }
            }
        }
        return dp[s.length()];
    }
    // leetcode    leet code

    //1=false     l
    //2=false     le e
    //3=false       lee ee e
    //4=true        [leet] eet et t
    //5=false       leetc ettc etc tc c
    //6=false       leetco ettco etco tco co o
    //7=false       leetcod ettcod etcod tcod cod od d
    //8=true        leetcode eetcode etcode tcode [code] ode de e
}
